package models.issue;

import models.UUIDModel;
import models.admin.AdminUser;
import models.constants.DeletedStatus;
import models.enums.IssueDetailStatus;
import models.enums.IssueStatus;
import models.enums.OperatorType;
import models.enums.ScoreLevel;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * Created by hanbin
 * on 2017/1/11.
 * Time : 上午1:11
 */
@Entity
@Table(name = "is_issue_details")
public class IssueDetail extends UUIDModel {

    /**
     * 所属issue
     */
    @JoinColumn(name = "issue_id")
    @ManyToOne
    public Issue issue;

    /**
     * 操作人员 的Id
     */
    @Column(name = "operator", length = 32)
    public String operator;

    @Transient
    public String operatorName;

    /**
     * 人员类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "operator_type", length = 10)
    public OperatorType operatorType;

    /**
     * 描述说明(报修说明,评价内容)
     */
    @Column(name = "remark", length = 100)
    public String remark;

    /**
     * 评价等级
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "score_level", length = 10)
    public ScoreLevel scoreLevel;

    /**
     * 状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "issue_status", length = 15)
    public IssueStatus issueStatus;

    /**
     * 报修详情状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "issue_detail_status",length = 15)
    public IssueDetailStatus issueDetailStatus = IssueDetailStatus.NORMAL;


    public IssueDetail() {
        super();
    }

    public IssueDetail(Issue issue, String userId,  OperatorType operatorType, String remark, IssueStatus issueStatus) {
        this.issue = issue;
        this.operator = userId;
        this.operatorType = OperatorType.USER;
        this.remark = remark;
        this.issueStatus = IssueStatus.CREATE;
        this.deleted = DeletedStatus.UN_DELETED;
        this.createdAt = new Date();
        this.belongCompany = issue.belongCompany != null ? issue.belongCompany : null;

    }

    /**
     * 查找未删除
     *
     * @param id
     * @return
     */
    public static List<IssueDetail> findByIssueId(String id) {
        return IssueDetail.find("issue.id = ?1 and deleted = ?2 order by createdAt Desc", id, DeletedStatus.UN_DELETED).fetch();
    }

    /**
     * 派工
     */
    public static void paiGong(Issue issue, String operator, Date now , AdminUser loginUser) {
        IssueDetail issueDetail = new IssueDetail();
        issueDetail.createdAt = now;
        issueDetail.issue = issue;
        issueDetail.operator = operator;
        issueDetail.operatorType = OperatorType.SYSUSER;
        issueDetail.issueStatus = IssueStatus.HANDLING;
        issueDetail.deleted = DeletedStatus.UN_DELETED;
        issueDetail.createUser = loginUser ;
        issueDetail.issueDetailStatus = IssueDetailStatus.NORMAL;
        issueDetail.belongCompany = loginUser.organization != null ? loginUser.organization : null ;
        issueDetail.save();
    }

    public static void callBack(Issue issue, String operator, ScoreLevel scoreLevel, String remark, Date now , AdminUser loginUser) {
        IssueDetail issueDetail = new IssueDetail();
        issueDetail.createdAt = now;
        issueDetail.issue = issue;
        issueDetail.operator = operator;
        issueDetail.operatorType = OperatorType.SYSUSER;
        issueDetail.issueStatus = IssueStatus.CALLBACKED;
        issueDetail.scoreLevel = scoreLevel;
        issueDetail.remark = remark;
        issueDetail.deleted = DeletedStatus.UN_DELETED;
        issueDetail.createUser = loginUser ;
        issueDetail.issueDetailStatus = IssueDetailStatus.NORMAL;
        issueDetail.belongCompany = loginUser.organization != null ? loginUser.organization : null;
        issueDetail.save();
    }

    public static void completed(Issue issue,Date now , AdminUser loginUser){
        IssueDetail issueDetail = new IssueDetail();
        issueDetail.createdAt = now;
        issueDetail.issue = issue;
        issueDetail.operator = loginUser.id.toString();
        issueDetail.operatorType = OperatorType.SYSUSER;
        issueDetail.issueStatus = IssueStatus.COMPLETED;
        issueDetail.deleted = DeletedStatus.UN_DELETED;
        issueDetail.createUser = loginUser;
        issueDetail.issueDetailStatus = IssueDetailStatus.NORMAL;
        issueDetail.belongCompany = loginUser.organization != null ? loginUser.organization : null ;
        issueDetail.save();
    }

    public static void repairManVisitDoor(Issue issue,Date now , String remark,AdminUser adminUser) {
        IssueDetail issueDetail = new IssueDetail();
        issueDetail.createdAt = now;
        issueDetail.issue = issue;
        issueDetail.remark = remark;
        issueDetail.operator = adminUser.id.toString();
        issueDetail.operatorType = OperatorType.SYSUSER;
        issueDetail.issueStatus = IssueStatus.REPAIRMANDOOR;
        issueDetail.deleted = DeletedStatus.UN_DELETED;
        issueDetail.createUser = adminUser;
        issueDetail.issueDetailStatus = IssueDetailStatus.NORMAL;
        issueDetail.belongCompany = adminUser.organization != null ? adminUser.organization : null ;
        issueDetail.save();
    }

    public static void repairManAccept(Issue issue,Date now ,AdminUser adminUser) {
        IssueDetail issueDetail = new IssueDetail();
        issueDetail.createdAt = now;
        issueDetail.issue = issue;
        issueDetail.operator = adminUser.id.toString();
        issueDetail.operatorType = OperatorType.SYSUSER;
        issueDetail.issueStatus = IssueStatus.REPAIRMANHANDLE;
        issueDetail.deleted = DeletedStatus.UN_DELETED;
        issueDetail.createUser = adminUser;
        issueDetail.issueDetailStatus = IssueDetailStatus.NORMAL;
        issueDetail.belongCompany = adminUser.organization != null ? adminUser.organization : null ;
        issueDetail.save();
    }

    // 安排工程经理上门
    public static void appointEngineer(Issue issue, String operator, Date now , AdminUser adminUser, AdminUser loginUser) {
        IssueDetail issueDetail = new IssueDetail();
        issueDetail.createdAt = now;
        issueDetail.issue = issue;
        issueDetail.operator = operator;
        issueDetail.operatorType = OperatorType.SYSUSER;
        issueDetail.issueStatus = IssueStatus.ACCEPT;
        issueDetail.deleted = DeletedStatus.UN_DELETED;
        issueDetail.createUser = loginUser ;
        issueDetail.issueDetailStatus = IssueDetailStatus.NORMAL;
        issueDetail.belongCompany = loginUser.organization != null ? loginUser.organization : null ;
        issueDetail.remark = "已安排工程经理:"+adminUser.name+"上门。";
        issueDetail.save();
    }

    public static void engineerVisit(Issue issue,Date now ,AdminUser adminUser) {
        IssueDetail issueDetail = new IssueDetail();
        issueDetail.createdAt = now;
        issueDetail.issue = issue;
        issueDetail.operator = adminUser.id.toString();
        issueDetail.operatorType = OperatorType.SYSUSER;
        issueDetail.issueStatus = IssueStatus.ENGINEERDOOR;
        issueDetail.deleted = DeletedStatus.UN_DELETED;
        issueDetail.createUser = adminUser;
        issueDetail.issueDetailStatus = IssueDetailStatus.NORMAL;
        issueDetail.belongCompany = adminUser.organization != null ? adminUser.organization : null ;
        issueDetail.save();
    }

    /**
     *
     * @param issueId
     * @param operatorType
     * @return
     */
    public static List<IssueDetail> findByIssueIdAndStatus(String issueId , OperatorType operatorType){
        return IssueDetail.find("deleted = 0 and issue.id = ? and issueDetailStatus = ?" , issueId ,operatorType ).fetch();
    }

}
